package tq.com.tqstudent.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import tq.com.tqstudent.App;
import tq.com.tqstudent.R;
import tq.com.tqstudent.bean.CommentBean;
import tq.com.tqstudent.utils.VarietyUtil;

/**
 * @Author rames
 * @date 2019/3/28 16:54
 */
public class CommentAdapter extends RecyclerView.Adapter {
    private List<CommentBean> mDatas = new ArrayList<>();
    private Context mContext;
    private View mHeaderView;
    private View mFooterView;
    private View mEmptyView;
    private OnItemClickListener listener;

    private int ITEM_TYPE_NORMAL = 0;
    private int ITEM_TYPE_HEADER = 1;
    private int ITEM_TYPE_FOOTER = 2;
    private int ITEM_TYPE_EMPTY = 3;

    public CommentAdapter(Context context) {
        mContext = context;
    }

    public void setData(List<CommentBean> datas) {
        mDatas = datas;
        notifyDataSetChanged();
    }

    public void addData(List<CommentBean> datas){
        mDatas.addAll(datas);
        notifyDataSetChanged();
    }

    public List<CommentBean> getDatas(){
        return mDatas;
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

    // 创建视图
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_HEADER) {
            return new ViewHolder(mHeaderView);
        } else if (viewType == ITEM_TYPE_EMPTY) {
            return new ViewHolder(mEmptyView);
        } else if (viewType == ITEM_TYPE_FOOTER) {
            return new ViewHolder(mFooterView);
        } else {
            View v = LayoutInflater.from(mContext).inflate( R.layout.item_comment, parent,false);
            return new ViewHolder(v);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (null != mHeaderView && position == 0) {
            return ITEM_TYPE_HEADER;
        }
        if (null != mFooterView && position == getItemCount() - 1) {
            return ITEM_TYPE_FOOTER;
        }
        if (null != mEmptyView && mDatas.size() == 0) {
            return ITEM_TYPE_EMPTY;
        }
        return ITEM_TYPE_NORMAL;

    }

    // 为Item绑定数据
    @Override
    public void onBindViewHolder (RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        if (type == ITEM_TYPE_HEADER || type == ITEM_TYPE_FOOTER || type == ITEM_TYPE_EMPTY) {
            return;
        }

        int realPos = getRealItemPosition(position);
        CommentBean bean = mDatas.get(realPos);
        ViewHolder vh = ((ViewHolder) holder);

        Glide.with(mContext).load(VarietyUtil.getHeadImgUri(bean.author.pic)).into(vh.headIV);
        vh.contentTV.setText(bean.content);
        vh.nameTV.setText(bean.author.name);
        vh.scoreTV.setText(String.valueOf(bean.score));
    }

    private int getRealItemPosition(int position) {
        if (null != mHeaderView) {
            return position - 1;
        }
        return position;
    }

    @Override
    public int getItemCount() {
        int itemCount = mDatas.size();
        if (null != mEmptyView && itemCount == 0) {
            itemCount++;
        }
        if (null != mHeaderView) {
            itemCount++;
        }
        if (null != mFooterView) {
            itemCount++;
        }
        return itemCount;
    }

    public void addHeaderView(View view) {
        mHeaderView = view;
        notifyItemInserted(0);
    }

    public void addFooterView(View view) {
        mFooterView = view;
        notifyItemInserted(getItemCount() - 1);
    }

    public void setEmptyView(View view) {
        mEmptyView = view;
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView contentTV, nameTV, scoreTV;
        ImageView headIV;

        ViewHolder(View v) {
            super(v);
            contentTV = v.findViewById(R.id.content_tv);
            nameTV = v.findViewById(R.id.name_tv);
            scoreTV = v.findViewById(R.id.score_tv);
            headIV = v.findViewById(R.id.head_iv);

            v.setOnClickListener(view -> {
                if(null != listener){
                    listener.onItemClick(v, getAdapterPosition());
                }
            });
        }
    }
}
